﻿using Castle.Windsor;
using good1.ApplicationServices;
using good1.ApplicationServices.Interface;
using good1.Web.Controllers.Infrastructure;
using good1.Web.Controllers.Infrastructure.Cookies;
using good1.Web.Controllers.Infrastructure.Session;
using SharpArch.Core.PersistenceSupport.NHibernate;
using SharpArch.Data.NHibernate;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Web.Castle;
using Castle.MicroKernel.Registration;
using SharpArch.Core.CommonValidator;
using SharpArch.Core.NHibernateValidator.CommonValidatorAdapter;

namespace good1.Web.CastleWindsor
{
    public class ComponentRegistrar
    {
        public static void AddComponentsTo(IWindsorContainer container)
        {
            AddGenericRepositoriesTo(container);
            AddCustomRepositoriesTo(container);
            AddApplicationServicesTo(container);
            AddCustomContainers(container);

            container.AddComponent("validator",
                typeof(IValidator), typeof(Validator));
        }

        private static void AddCustomRepositoriesTo(IWindsorContainer container)
        {
            container.Register(
                AllTypes.Pick()
                .FromAssemblyNamed("good1.Data")
                .WithService.FirstNonGenericCoreInterface("good1.Core"));
        }

        private static void AddGenericRepositoriesTo(IWindsorContainer container)
        {
            container.AddComponent("entityDuplicateChecker",
                typeof(IEntityDuplicateChecker), typeof(EntityDuplicateChecker));
            container.AddComponent("repositoryType",
                typeof(IRepository<>), typeof(Repository<>));
            container.AddComponent("nhibernateRepositoryType",
                typeof(INHibernateRepository<>), typeof(NHibernateRepository<>));
            container.AddComponent("repositoryWithTypedId",
                typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
            container.AddComponent("nhibernateRepositoryWithTypedId",
                typeof(INHibernateRepositoryWithTypedId<,>), typeof(NHibernateRepositoryWithTypedId<,>));
        }

        private static void AddApplicationServicesTo(IWindsorContainer container)
        {
            container.Register(
                AllTypes.Pick()
                .FromAssemblyNamed("good1.ApplicationServices")
                .WithService.FirstInterface());
        }

        private static void AddCustomContainers(IWindsorContainer container)
        {
            // if this grows then add a common interface to add them together
            container.AddComponent("cookieContainers",
                typeof(ICookieContainer), typeof(CookieContainer));
            container.AddComponent("goodCookies",
                typeof(IGoodCookieService), typeof(GoodCookies));
            container.AddComponent("SecurityContext",
                typeof(ISecurityContextService), typeof(SecurityContextService));
            container.AddComponent("HttpContextSessionContainer", typeof(ISessionContainer), typeof(HttpContextSessionContainer))
            ;
        }

    
    }
}
